<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%system_group}}".
 *
 * @property int $id
 * @property int $pid 父ID
 * @property string $path 路径
 * @property string $name 管理组名称
 * @property string $desc 描述
 * @property int $status 状态, 0:禁用 1:正常
 * @property int $assigned 已分配权限, 0:否 1:是
 * @property string $created_at
 * @property string|null $updated_at
 */
class SystemGroup extends \builder\base\BaseActiveRecord
{
    const STATUS_NORMAL = 1;

    const STATUS_DENY = 0;

    /**
     * 当前组是否允许创建其子分组。
     * @return bool
     */
    public function isAllowCreateChild():bool
    {
        return $this->status && $this->assigned;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%system_group}}';
    }

    /**
     * 定义场景
     * @return array
     */
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        //新增
        $scenarios['SCE_Group_Add'] = ['pid', 'name', 'desc', 'status', 'path'];
        //编辑
        $scenarios['SCE_Group_Update'] = ['pid', 'name', 'desc', 'status', 'path'];

        return $scenarios;
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'status', 'pid'], 'required'],
            [['pid', 'status', 'assigned'], 'integer'],
            [['status'], 'in', 'range' => [self::STATUS_DENY, self::STATUS_NORMAL]],
            [['created_at', 'updated_at'], 'safe'],
            [['path', 'desc'], 'string', 'max' => 255],
            [['name'], 'string', 'max' => 50],
            [['name'], 'unique'],
            [['desc'], 'default', 'value' => ''],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'pid' => '父ID',
            'path' => '路径',
            'name' => '管理组',
            'desc' => '描述',
            'status' => '状态',
            'assigned' => '已分配权限',
            'created_at' => '添加时间',
            'updated_at' => '上次更新',
        ];
    }
}
